mini.parse();
var types=[{id:"int",text:"整型"},
    {id:"decimal",text:"数字"},
    {id:"varchar(max)",text:"字符串"},
    {id:"text",text:"文本"},
    {id:"date",text:"日期"},
    {id:"datetime",text:"日期时间"}];
var grid = mini.get("datagrid1");
var grid2 = mini.get("datagrid2");
grid.load({tablename:"Table_Main"});


//////////////////////////////////////////////////////
function  reloaddata() {
    grid.reload();
}
function search() {
    var key = mini.get("key").getValue();

    grid.load({ key: key });
}

function onKeyEnter(e) {
    search();
}

function addRow() {
    var newRow = { cTableName: "" };
    grid.addRow(newRow, 0);

    grid.beginEditCell(newRow, "cTableName");
    grid.setSelected(newRow);
    grid2.clearRows();

}
function removeRow() {
    var rows = grid.getSelecteds();
    if (rows.length > 0) {

        grid.removeRows(rows, true);
    }
}
function saveData() {
    grid.commitEdit();
    grid2.commitEdit();
    var rowData = grid.getChanges();
    if(rowData.length>0){
        var rowData_D = grid2.getChanges();
        grid.loading("保存中，请稍后......");
        grid2.loading("保存中，请稍后......");
        var json = mini.encode({"tablename": "table_main","data":rowData[0],"detail":[{"tablename": "table_info","data":rowData_D}]});
        $.ajax({
            type:"post",
            url: "../../dsaction/savedata",
            data: {info:json},
            success: function (text) {
                var reJson=JSON.parse(text);
                if(reJson.success=="0"){
                    mini.alert(reJson.msg);
                }
                grid.reload();
                getup();
            },
            error: function (jqXHR, textStatus, errorThrown) {
                mini.alert(jqXHR.responseText);
            }
        });
    }else{
        mini.alert("没有需要保存的数据！");
    }
}


grid.on("celleditenter", function (e) {
    var index = grid.indexOf(e.record);
    if (index == grid.getData().length - 1) {
        var row = {};
        grid.addRow(row);
    }
});

grid.on("beforeload", function (e) {
    if (grid.getChanges().length > 0) {
        if (confirm("有增删改的数据未保存，是否取消本次操作？")) {
            e.cancel = true;
        }
    }
});

var editWindow = mini.get("editWindow");
function onButtonEdit() {
    var row = grid.getSelected();
    if (row) {
        mini.get("txt_sql").setValue(row.cViewSql);
        editWindow.show();
    }
}
function refok() {
    var row = grid.getSelected();
    row.cViewSql=mini.get("txt_sql").value;
    grid.updateRow(row);
    editWindow.hide();
}
function  gd1select(sender) {
    if(!mini.get("but_saveData").enabled){
        var row =sender.record;
        grid2.load({tablename:"Table_Info",cWhereStr:" and Table_main_id="+row.id+""});
    }else{
    }

}
function update() {
    grid.allowCellEdit=true;
    grid2.allowCellEdit=true;
    grid.allowRowSelect=false;
    mini.get("but_update").disable();
    mini.get("but_saveData").enable();
    mini.get("but_removeRow").enable();
    mini.get("but_addRow").enable();
    mini.get("but_reload").disable();
    mini.get("but_getup").enable();
    mini.get("but_addRow_D").enable();
    mini.get("but_removeRow_D").enable();
}
function getup() {
    grid.allowCellEdit=false;
    grid2.allowCellEdit=false;
    grid.allowRowSelect=true;
    mini.get("but_update").enable();
    mini.get("but_saveData").disable();
    mini.get("but_removeRow").disable();
    mini.get("but_addRow").disable();
    mini.get("but_getup").disable();
    mini.get("but_reload").enable();
    mini.get("but_addRow_D").disable();
    mini.get("but_removeRow_D").disable();
}
function addRow_D() {
    var row = grid.getSelected();
    var newRow = { Table_Main_id:0,bNull:false,bKey:false,bAuto:false};
    if (typeof(row.id) == "undefined"){
        newRow = { Table_Main_id:row.id,bNull:false,bKey:false,bAuto:false};
    }
    grid2.addRow(newRow, 0);
    grid2.beginEditCell(newRow, "Table_Main_id");
}
function removeRow_D() {
    var rows = grid2.getSelecteds();
    if (rows.length > 0) {
        grid2.removeRows(rows, true);
    }
}